<?php

declare(strict_types=1);

namespace app\cart\controller;

use app\cart\model\GoodsShopcar;
use app\CheckTokenController;

class Index extends CheckTokenController
{
    public function index()
    {
        $params = $this->request->param();
        $DBGoodsShopcart = new GoodsShopcar();
        $info = $DBGoodsShopcart->where('goods_id', $params['goods_id'])->find();
        if (!$info) {
            $DBGoodsShopcart->save($params);
            $info = $DBGoodsShopcart->where('goods_id', $params['goods_id'])->find();
        } else {
            // 已经存在商品，让number+
            $updateInfo =  $DBGoodsShopcart->where('goods_id', $params['goods_id'])->update(['number' => $info['number'] + $params['number']]);
            if ($updateInfo) {
                $info = $DBGoodsShopcart->where('goods_id', $params['goods_id'])->find();
                return result($info, '添加购物车成功');
            }
        }
        return result($info);
    }
}